#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFile>
#include <QCoreApplication>
#include <QRandomGenerator>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    ,m_ptrStuSql(nullptr)
{
    ui->setupUi(this);

    QFile dlgCss;
    dlgCss.setFileName(":/css/dlg.css");
    dlgCss.open(QIODevice::ReadOnly);
    QString strQss = dlgCss.readAll();
    //qDebug()<<strQss;
    m_dlgLogin.setStyleSheet(strQss);
    dlgCss.close();

    m_dlgLogin.show();

    // auto f = [&]()
    // {
    //     this->show();
    // };



    connect(&m_dlgLogin, &PageLog::sendLoginSucess, this, [=](){

        QFile mainCss;
        mainCss.setFileName(":/css/main.css");
        mainCss.open(QIODevice::ReadOnly);
        QString mainQss = mainCss.readAll();
        this->setStyleSheet(mainQss);
        mainCss.close();
        this->show();
    });

    ui->treeWidget->clear();
    ui->treeWidget->setColumnCount(1);
    QTreeWidgetItem *pf = new QTreeWidgetItem(ui->treeWidget, QStringList("学生信息管理系统"));
    QTreeWidgetItem *p1 = new QTreeWidgetItem(pf, QStringList("学生管理"));
    QTreeWidgetItem *p2 = new QTreeWidgetItem(pf, QStringList("管理员管理"));


    ui->treeWidget->addTopLevelItem(pf);

    pf->addChild(p1);
    pf->addChild(p2);
    ui->treeWidget->expandAll();
    ui->stackedWidget->setCurrentIndex(0);

    m_ptrStuSql = stuSql::getinstance();
    m_ptrStuSql->init();

    //m_ptrStuSql->getAllUser();

    //ui->tableWidget->clear();

    updateTable();
}

MainWindow::~MainWindow()
{
    m_ptrStuSql->closedb();
    delete ui;
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_F6)
    {
        QFile mainCss;
        mainCss.setFileName(":/css/main.css");
        mainCss.open(QIODevice::ReadOnly);
        QString mainQss = mainCss.readAll();
        this->setStyleSheet(mainQss);
        mainCss.close();
    }
}

void MainWindow::on_pushButton_clicked()
{
    exit(0);
}


void MainWindow::on_btn_simulation_clicked()
{
    //制作1000条数据
    QRandomGenerator g, c;
    g.seed(0);
    c.seed(1);

    QList<StuInfo> l;

    for (int i = 0; i < 1000; ++i) {
        StuInfo info;

        auto grade = g.bounded(1, 4);
        auto cl = c.bounded(1, 6);

        info.name = QString("张%1").arg(i);

        if(i%3)
        {

            info.age =13;

        }
        if(i%7)
        {
            info.age =17;
        }
        if(i%2)
        {
            info.age =12;
        }

        info.grade = 1;

        info.grade = grade;
        info.uiclass = cl;
        info.stuid= i;
        info.phone = "111111111";
        info.wechat = "12222222222";
        l.append(info);
    }
    m_ptrStuSql->addStu(l);

    updateTable();
}


void MainWindow::on_btn_add_clicked()
{
    m_dlgAddStu.setType(true);
    m_dlgAddStu.exec();
    updateTable();
}

void MainWindow::on_btn_clear_clicked()
{
    m_ptrStuSql->clearStuTable();
    updateTable();

}

void MainWindow::updateTable()
{
    ui->tableWidget->clear();

    ui->tableWidget->setColumnCount(9);

    QStringList l;

    l<<"序号"<<"id"<<"姓名"<<"年龄"<<"年级"<<"班级"<<"学号"<<"电话"<<"微信";

    ui->tableWidget->setHorizontalHeaderLabels(l);

    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    //ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    auto cnt = m_ptrStuSql->getStuCount();
    ui->lb_num->setText(QString("学生总数：%1").arg(cnt));

    QList<StuInfo> lstudents = m_ptrStuSql->getPageStu(0, cnt);

    ui->tableWidget->setRowCount(cnt);

    for (int i = 0; i<lstudents.size(); i++)
    {
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(QString::number(i)));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(lstudents[i].id)));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(lstudents[i].name));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(QString::number(lstudents[i].age)));
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(lstudents[i].grade)));
        ui->tableWidget->setItem(i, 5, new QTableWidgetItem(QString::number(lstudents[i].uiclass)));
        ui->tableWidget->setItem(i, 6, new QTableWidgetItem(QString::number(lstudents[i].stuid)));
        ui->tableWidget->setItem(i, 7, new QTableWidgetItem(lstudents[i].phone));
        ui->tableWidget->setItem(i, 8, new QTableWidgetItem(lstudents[i].wechat));

    }
}


void MainWindow::on_btn_del_clicked()
{
    int i = ui->tableWidget->currentRow();

    if(i >= 0)
    {
        int id = ui->tableWidget->item(i, 1)->text().toInt();
        m_ptrStuSql->delStu(id);
        updateTable();

        QMessageBox::information(nullptr, "信息", "删除成功");
    }
}


void MainWindow::on_btn_update_clicked()
{
    StuInfo info;

    int i = ui->tableWidget->currentRow();

    if(i >= 0)
    {
        info.id = ui->tableWidget->item(i, 1)->text().toInt();
        info.name = ui->tableWidget->item(i, 2)->text();
        info.age = ui->tableWidget->item(i, 3)->text().toInt();
        info.grade = ui->tableWidget->item(i, 4)->text().toInt();
        info.uiclass = ui->tableWidget->item(i, 5)->text().toInt();
        info.stuid = ui->tableWidget->item(i, 6)->text().toInt();
        info.phone = ui->tableWidget->item(i, 7)->text();
        info.wechat = ui->tableWidget->item(i, 8)->text();


        m_dlgAddStu.setType(false, info);
        m_dlgAddStu.exec();
    }


    updateTable();

}


void MainWindow::on_btn_search_clicked()
{
    QString strFilter = ui->le_search->text();
    if(strFilter.isEmpty())
    {
        QMessageBox::information(nullptr, "警告", "搜索名字不能为空");
        updateTable();
        return;
    }

    ui->tableWidget->clear();

    ui->tableWidget->setColumnCount(9);

    QStringList l;

    l<<"序号"<<"id"<<"姓名"<<"年龄"<<"年级"<<"班级"<<"学号"<<"电话"<<"微信";

    ui->tableWidget->setHorizontalHeaderLabels(l);

    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    //ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    auto cnt = m_ptrStuSql->getStuCount();
    ui->lb_num->setText(QString("学生总数：%1").arg(cnt));

    QList<StuInfo> lstudents = m_ptrStuSql->getPageStu(0, cnt);

    ui->tableWidget->setRowCount(cnt);

    int index = 0;

    for (int i = 0; i<lstudents.size(); i++)
    {
        if(!lstudents[i].name.contains(strFilter))
        {
            continue;
        }

        ui->tableWidget->setItem(index, 0, new QTableWidgetItem(QString::number(i)));
        ui->tableWidget->setItem(index, 1, new QTableWidgetItem(QString::number(lstudents[i].id)));
        ui->tableWidget->setItem(index, 2, new QTableWidgetItem(lstudents[i].name));
        ui->tableWidget->setItem(index, 3, new QTableWidgetItem(QString::number(lstudents[i].age)));
        ui->tableWidget->setItem(index, 4, new QTableWidgetItem(QString::number(lstudents[i].grade)));
        ui->tableWidget->setItem(index, 5, new QTableWidgetItem(QString::number(lstudents[i].uiclass)));
        ui->tableWidget->setItem(index, 6, new QTableWidgetItem(QString::number(lstudents[i].stuid)));
        ui->tableWidget->setItem(index, 7, new QTableWidgetItem(lstudents[i].phone));
        ui->tableWidget->setItem(index, 8, new QTableWidgetItem(lstudents[i].wechat));

        index++;
    }

}

